package com.wechat.core.service.impl;

import com.wechat.core.common.WeChatContant;
import com.wechat.core.model.ArticleItem;
import com.wechat.core.service.UserInfoService;
import com.wechat.core.service.WeChatService;
import com.wechat.core.util.WeChatUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author hgb
 * @date 2019/1/26
 * Email xhy18650@sina.com
 */
@Service
public class WeChatServiceImpl implements WeChatService {

    private Logger logger = LoggerFactory.getLogger(WeChatServiceImpl.class);

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public String processRequest(HttpServletRequest request) {
        // xml格式的消息数据
        String respXml = null;
        // 默认返回的文本消息内容
        String respContent;
        try {
            // 调用parseXml方法解析请求消息
            Map<String, String> requestMap = WeChatUtil.parseXml(request);
            // 消息类型
            String msgType = (String) requestMap.get(WeChatContant.MsgType);
            String mes = null;
            // 文本消息
            if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_TEXT)) {
                mes = requestMap.get(WeChatContant.Content).toString();
                if (mes != null && mes.length() < 2) {
                    List<ArticleItem> items = new ArrayList<>();
                    ArticleItem item = null;
                    item = new ArticleItem();
                    item.setTitle("小游戏");
                    item.setDescription("小游戏");
                    item.setPicUrl("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3257434623,3478200329&fm=26&gp=0.jpg");
                    item.setUrl("https://mp.weixin.qq.com/s/4P2e67VidADWR63V8Jm61w");
                    items.add(item);

                    item = new ArticleItem();
                    item.setTitle("百度");
                    item.setDescription("百度一下");
                    item.setPicUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505100912368&di=69c2ba796aa2afd9a4608e213bf695fb&imgtype=0&src=http%3A%2F%2Ftx.haiqq.com%2Fuploads%2Fallimg%2F170510%2F0634355517-9.jpg");
                    item.setUrl("http://www.baidu.com");
                    items.add(item);
                    respXml = WeChatUtil.sendArticleMsg(requestMap, items);
                }
            }
            // 图片消息
            else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_IMAGE)) {
                respContent = "您发送的是图片消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 语音消息
            else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_VOICE)) {
                respContent = "您发送的是语音消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 视频消息
            else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_VIDEO)) {
                respContent = "您发送的是视频消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 地理位置消息
            else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_LOCATION)) {
                respContent = "您发送的是地理位置消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 链接消息
            else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_LINK)) {
                respContent = "您发送的是链接消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 事件推送
            else if (msgType.equals(WeChatContant.REQ_MESSAGE_TYPE_EVENT)) {
                // 事件类型
                String eventType = (String) requestMap.get(WeChatContant.Event);
                // 关注
                if (eventType.equals(WeChatContant.EVENT_TYPE_SUBSCRIBE)) {
                    respContent = "谢谢您的关注！";
                    respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
                }
                // 取消关注
                else if (eventType.equals(WeChatContant.EVENT_TYPE_UNSUBSCRIBE)) {
                    // TODO 取消订阅后用户不会再收到公众账号发送的消息，因此不需要回复
                }
                // 扫描带参数二维码
                else if (eventType.equals(WeChatContant.EVENT_TYPE_SCAN)) {
                    // TODO 处理扫描带参数二维码事件
                }
                // 上报地理位置
                else if (eventType.equals(WeChatContant.EVENT_TYPE_LOCATION)) {
                    // TODO 处理上报地理位置事件
                }
                // 自定义菜单
                else if (eventType.equals(WeChatContant.EVENT_TYPE_CLICK)) {
                    // TODO 处理菜单点击事件
                    String eventKey = requestMap.get(WeChatContant.EVENT_KEY);
                    if("V1001_TODAY_MUSIC".equals(eventKey)){
                        //每日一签{向用户推送用户个人信息}
                        Map<String, Object> userInfo = userInfoService.getUserInfo(requestMap.get(WeChatContant.FromUserName));
                        logger.debug(userInfo.toString());
                        String nickname = userInfo.get("nickname").toString();
                        String city = userInfo.get("city").toString();
                        String province = userInfo.get("province").toString();
                        String country = userInfo.get("country").toString();
                        String headimgurl = userInfo.get("headimgurl").toString();
                        List<ArticleItem> items = new ArrayList<>();
                        ArticleItem item = new ArticleItem();
                        item.setTitle(nickname+"签到");
                        item.setDescription("昵称:" + nickname + "  地址:" + country + " " + province + " " + city);
                        item.setPicUrl(headimgurl);
                        item.setUrl("http://www.baidu.com");
                        items.add(item);
                        respXml = WeChatUtil.sendArticleMsg(requestMap, items);
                    }
                    else  if("V1001_OUT".equals(eventKey)){
                        //关于我们
                        respXml = WeChatUtil.sendTextMsg(requestMap, "关于我们");
                    }
                    else  if("V1001_MSG".equals(eventKey)){
                        //留言
                        respXml = WeChatUtil.sendTextMsg(requestMap, "留言");
                    }

                }
            }
            mes = mes == null ? "" : mes;
            if (respXml == null){
                respXml = WeChatUtil.sendTextMsg(requestMap, mes);
            }

            logger.debug(respXml);
            return respXml;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
